C51 COMPILER V9.52.0.0   MAIN                                                              09/28/2022 19:51:28 PAGE 1   


C51 COMPILER V9.52.0.0, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN .\Release\main.obj
COMPILER INVOKED BY: C:\Keil_v4\C51\BIN\C51.EXE main.c OPTIMIZE(0,SPEED) REGFILE(.\Release\Sample_DrvI2C_Master.ORC) BRO
                    -WSE INCDIR(..\..\..\..\C51SIS\C51;..\..\..\Include\Driver) DEBUG OBJECTEXTEND TABS(2) OBJECT(.\Release\main.obj)

line level    source

   1          /**************************************************************************//**
   2           * @file           main.c
   3           * @version        V1.0
   4           * $Revision:      1 $
   5           * $Date:          2017/01/04 $
   6           * @brief          Main function
   7           * @Author         Jeffrey Kang
   8           * @ContactInfo    chkang@esmt.com.tw
   9           *
  10           * @note
  11           *
  12           * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
  13           * All rights reserved.
  14           *
  15           ******************************************************************************/
  16          
  17          
  18          #include <REG52.h>
  19          #include "system_FE81001.h"
  20          
  21          unsigned char cnt = 0;
  22          
  23          unsigned char I2C_TX_Buffer[10] = {0};    // Transfer data buffer
  24          unsigned char I2C_RX_Buffer[10] = {0};  // Receive data buffer
  25          
  26          
  27          
  28          
  29          /**
  30           * @brief       delay function
  31           *
  32           * @param       None
  33           *
  34           * @return      None
  35           *
  36           * @details     1. 
  37           */
  38          void delay (int cnt )
  39          {
  40   1        unsigned int i , j ;
  41   1        
  42   1        for(j=0 ; j<cnt ; j++)
  43   1        for( i = 0 ; i < 200 ; i++ ); 
  44   1        
  45   1      }
  46          
  47          
  48          
  49          /**
  50           * @brief       I2CSISR function
  51           *
  52           * @param       None
  53           *
  54           * @return      None
C51 COMPILER V9.52.0.0   MAIN                                                              09/28/2022 19:51:28 PAGE 2   

  55           *
  56           * @details     1. 
  57           */
  58          const void I2CISR (void) interrupt ISR_I2C
  59          {
  60   1        unsigned short int_stat;
  61   1        
  62   1        int_stat = (I2C->int_st[1] << 8) | I2C->int_st[0];
  63   1        
  64   1            
  65   1        /* FIFO full : read data here */
  66   1         if ((int_stat & 0x02) == 0x02){
  67   2        }
  68   1        
  69   1        /* Cmpl */
  70   1        else if ((int_stat & 0x0200) == 0x0200){
  71   2          I2C->int_st[1] |= 0x02;
  72   2            
  73   2        }
  74   1        
  75   1        /* FIFO empty : set data here */
  76   1        else if ((int_stat & 0x01) == 0x01){
  77   2        }
  78   1        
  79   1        /* enable global interrupt */
  80   1        INTC->int_gmask = 0x01;
  81   1      }
  82          
  83          
  84          /**
  85           * @brief       IOConfig function
  86           *
  87           * @param       None
  88           *
  89           * @return      None
  90           *
  91           * @details     1. 
  92           */
  93          void IOConfig()
  94          {
  95   1        /* enable P0.4, P0.5 open drain */
  96   1        PORT_SetPAOpenDrain(BIT4 | BIT5);
  97   1        
  98   1        
  99   1        /* enable P0.4(SCL), P0.5(SDA) function */
 100   1        Set_PA4_SDA();
 101   1        Set_PA5_SCL();  
 102   1        
 103   1          /* Set PC7 input as button */
 104   1          PORT_SetPCInput(BIT7);
 105   1      
 106   1          /* Set PD1,PD2,PD3 output */
 107   1          PORT_SetPDPushPull(BIT1 | BIT2 | BIT3 );
 108   1      }
 109          
 110          
 111          /**
 112           * @brief       CLKConfig function
 113           *
 114           * @param       None
 115           *
 116           * @return      None
C51 COMPILER V9.52.0.0   MAIN                                                              09/28/2022 19:51:28 PAGE 3   

 117           *
 118           * @details     1. 
 119           */
 120          void CLKConfig()
 121          {
 122   1        /* enable GPIO clock */
 123   1        PORT_PAPBPCPD_Clock_Enable();
 124   1        
 125   1        /* enable I2C clock */
 126   1        En_I2C_clk();
 127   1        
 128   1      }
 129          
 130          /**
 131           * @brief       I2C_Master_Write   function
 132           *
 133           * @param       data_ :  Transfer data
 134                                  len     :   Length of transfer buffer
 135                                  slave_address :   Slavess address
 136           *
 137           * @return      None
 138           *
 139           * @details     1. 
 140           */
 141          
 142          void I2C_Master_Write(unsigned char *data_ , unsigned char len , unsigned char slave_address )
 143          {
 144   1        unsigned char  i ; 
 145   1      
 146   1        I2C->i2c_cmd = 0x05; // Reset I2C control 
 147   1      
 148   1        /* i2c setup */
 149   1        I2C->i2c_setup[0] = 0x05;         // Master = 1, Addressing = 0, IICEN = 1
 150   1        
 151   1        /* set slave address */
 152   1        I2C->slv_adr[0] = slave_address; 
 153   1        I2C->slv_adr[1] = 0x00;
 154   1        
 155   1        /* interrupt enable */
 156   1        I2C->int_en[0] = 0x00;            // ByteTrans = 0, Start = 0, Stop = 0, ArbLose = 0, AddrHit = 0, FIFOHa
             -lf = 0, FIFOFull = 0, FIFOEmpty = 1
 157   1        I2C->int_en[1] = 0x00;            // Cmpl = 1, ByteRecv = 0
 158   1        //INTC->int_gmask = 0x01;
 159   1        
 160   1        /* set i2c start */
 161   1        I2C->i2c_ctrl = 0x10;             // Phase_start = 1, Phase_addr = 0, Phase_data = 0, Phase_stop = 0, Dir
             - = 0 (master transmitter)
 162   1        I2C->i2c_cmd = 0x01;
 163   1        while((I2C->int_st[0] & 0x40) != 0x40){}
 164   1        I2C->int_st[0] = 0x40;
 165   1        
 166   1        /* set i2c slave address */
 167   1        I2C->i2c_ctrl = 0x08;             // Phase_start = 0, Phase_addr = 1, Phase_data = 0, Phase_stop = 0, Dir
             - = 0 (master transmitter)
 168   1        I2C->i2c_cmd = 0x01;
 169   1        while((I2C->int_st[0] & 0x08) != 0x08){}
 170   1        I2C->int_st[0] = 0x08;
 171   1        
 172   1        /* set i2c data */
 173   1        for(i=0 ; i<len ; i++)
 174   1        {
 175   2          I2C->i2c_ctrl = 0x04;             // Phase_start = 0, Phase_addr = 0, Phase_data = 1, Phase_stop = 0, Di
C51 COMPILER V9.52.0.0   MAIN                                                              09/28/2022 19:51:28 PAGE 4   

             -r = 0 (master transmitter)
 176   2          I2C->datcnt = 1 ;
 177   2          I2C->i2c_dat = *(data_+i);
 178   2          I2C->i2c_cmd = 0x01;
 179   2         while((I2C->int_st[0] & 0x80) != 0x80){}
 180   2        I2C->int_st[0] = 0x80;  
 181   2        }
 182   1        
 183   1        /* set i2c stop */
 184   1        I2C->i2c_ctrl = 0x02;             // Phase_start = 0, Phase_addr = 0, Phase_data = 0, Phase_stop = 1, Dir
             - = 0 (master transmitter)
 185   1        I2C->i2c_cmd = 0x01;
 186   1        while((I2C->int_st[0] & 0x20) != 0x20){}
 187   1        I2C->int_st[0] = 0x20;
 188   1        
 189   1      }
 190          
 191          /**
 192           * @brief       I2C_Master_Read  function
 193           *
 194           * @param       data_       :  Transfer data
 195                                  len_W     :   Length of transfer buffer
 196                                  len_R       :   Length of receive buffer
 197                                  slave_address :   Slavess address
 198           *
 199           * @return      None
 200           *
 201           * @details     1. 
 202           */
 203          
 204          void I2C_Master_Read(unsigned char *data_ , unsigned char len_W ,unsigned char len_R , unsigned char slave
             -_address)
 205          {
 206   1        
 207   1        unsigned char  i ; 
 208   1      //---------WRITE-------------------//
 209   1      
 210   1        I2C->i2c_cmd = 0x05; // Reset I2C
 211   1        
 212   1        /* i2c setup */
 213   1        I2C->i2c_setup[0] = 0x05;         // Master = 1, Addressing = 0, IICEN = 1
 214   1        
 215   1        /* set slave address */
 216   1        I2C->slv_adr[0] = slave_address;  //slave_address
 217   1        I2C->slv_adr[1] = 0x00;
 218   1        
 219   1        /* interrupt enable */
 220   1        I2C->int_en[0] = 0x00;            // ByteTrans = 0, Start = 0, Stop = 0, ArbLose = 0, AddrHit = 0, FIFOHa
             -lf = 0, FIFOFull = 0, FIFOEmpty = 1
 221   1        I2C->int_en[1] = 0x00;            // Cmpl = 1, ByteRecv = 0
 222   1        //INTC->int_gmask = 0x01;
 223   1        
 224   1        /* set i2c start */
 225   1        I2C->i2c_ctrl = 0x10;             // Phase_start = 1, Phase_addr = 0, Phase_data = 0, Phase_stop = 0, Dir
             - = 0 (master transmitter)
 226   1        I2C->i2c_cmd = 0x01;
 227   1        while((I2C->int_st[0] & 0x40) != 0x40){}
 228   1        I2C->int_st[0] = 0x40;
 229   1        
 230   1        /* set i2c slave address */
 231   1        I2C->i2c_ctrl = 0x08;             // Phase_start = 0, Phase_addr = 1, Phase_data = 0, Phase_stop = 0, Dir
             - = 0 (master transmitter)
C51 COMPILER V9.52.0.0   MAIN                                                              09/28/2022 19:51:28 PAGE 5   

 232   1        I2C->i2c_cmd = 0x01;
 233   1        while((I2C->int_st[0] & 0x08) != 0x08){}
 234   1        I2C->int_st[0] = 0x08;
 235   1        
 236   1        /* set i2c data */
 237   1        
 238   1        for(i=0 ; i<len_W ; i++)
 239   1        {
 240   2          I2C->i2c_cmd = 0x04;
 241   2          I2C->i2c_ctrl = 0x04;             // Phase_start = 0, Phase_addr = 0, Phase_data = 1, Phase_stop = 0, Di
             -r = 0 (master transmitter)
 242   2          I2C->datcnt = 1 ;
 243   2          
 244   2          I2C->i2c_dat = *(data_+i);
 245   2          I2C->i2c_cmd = 0x01;
 246   2          while((I2C->int_st[0] & 0x80) != 0x80){}
 247   2          I2C->int_st[0] = 0x80;  
 248   2        }
 249   1      
 250   1      //--------- READ-------------------//
 251   1      
 252   1        I2C->int_en[1] = 0x01;            // Cmpl = 1, ByteRecv = 0 
 253   1          
 254   1        /* i2c setup */
 255   1        I2C->i2c_setup[0] = 0x05;         // Master = 1, Addressing = 0, IICEN = 1
 256   1        
 257   1        /* set i2c configuration */
 258   1        I2C->datcnt   = 1;
 259   1        
 260   1        /* set slave address */
 261   1        I2C->slv_adr[0] = slave_address;
 262   1        I2C->slv_adr[1] = 0x00;
 263   1          
 264   1        /* set i2c start */
 265   1        I2C->i2c_ctrl = 0x10;             // Phase_start = 1, Phase_addr = 0, Phase_data = 0, Phase_stop = 0, Dir
             - = 0 (master transmitter)
 266   1        I2C->i2c_cmd = 0x01;
 267   1        while((I2C->int_st[0] & 0x40) != 0x40){}
 268   1        I2C->int_st[0] = 0x40;
 269   1        
 270   1        /* set i2c slave address */
 271   1        I2C->i2c_ctrl = 0x09;             // Phase_start = 0, Phase_addr = 1, Phase_data = 0, Phase_stop = 0, Dir
             - = 1 (master transmitter)
 272   1        I2C->i2c_cmd = 0x01;
 273   1        while((I2C->int_st[0] & 0x08) != 0x08){}
 274   1        I2C->int_st[0] = 0x08;
 275   1        
 276   1        I2C->i2c_ctrl = 0x05;   
 277   1        I2C->datcnt = 1;  
 278   1        I2C->i2c_cmd = 0x01;
 279   1        
 280   1        for( i = 0 ;i<len_R ; i ++){
 281   2          
 282   2        while((I2C->int_st[1] & 0x01) != 0x01){} 
 283   2        I2C->int_st[1] |= 0x01; 
 284   2          
 285   2        I2C->i2c_ctrl = 0x01;
 286   2        
 287   2          if(i == len_R-1)
 288   2          { 
 289   3            I2C->i2c_cmd = 0x03; // cmd = 0x02 :ack  0x03 :nack
 290   3          }
C51 COMPILER V9.52.0.0   MAIN                                                              09/28/2022 19:51:28 PAGE 6   

 291   2          else
 292   2          {
 293   3              I2C->i2c_cmd = 0x02; // cmd = 0x02 :ack  0x03 :nack
 294   3          }
 295   2        
 296   2          I2C_RX_Buffer[i] =I2C->i2c_dat ;  
 297   2          
 298   2        }
 299   1          
 300   1        /* set i2c stop */
 301   1        I2C->i2c_ctrl = 0x02;             // Phase_start = 0, Phase_addr = 0, Phase_data = 0, Phase_stop = 1, Dir
             - = 0 (master transmitter)
 302   1        I2C->i2c_cmd = 0x01;
 303   1        while((I2C->int_st[0] & 0x20) != 0x20){}
 304   1        I2C->int_st[0] = 0x20;
 305   1        
 306   1      }
 307          
 308          void I2C_Master_Config()
 309          {
 310   1          CLKConfig();
 311   1          IOConfig();
 312   1          I2C_speed_400k_8M();
 313   1        
 314   1      }
 315          
 316          /**
 317           * @brief       main function
 318           *
 319           * @param       None
 320           *
 321           * @return      None
 322           *
 323           * @details     1. 
 324           */
 325          int main (void)
 326          {
 327   1         I2C_Master_Config();
 328   1        
 329   1          PD1 = 1; // LED_G1 Off
 330   1          PD2 = 1 ; //LED_R Off
 331   1          PD3=  1;  // LED_G2  Off
 332   1        
 333   1        // Master Tx Test
 334   1        I2C_TX_Buffer[0] = 0x01;
 335   1        I2C_TX_Buffer[1] = 0x02;
 336   1        I2C_TX_Buffer[2] = 0x03;
 337   1        I2C_Master_Write(I2C_TX_Buffer,3,0x57);
 338   1      
 339   1        // Master Rx Test
 340   1        I2C_TX_Buffer[0] = 0x00; 
 341   1        I2C_Master_Read(I2C_TX_Buffer,1,2,0x57);  // Receive data in " I2C_RX_Buffer "
 342   1        
 343   1        
 344   1        
 345   1        while(1)
 346   1          
 347   1        {
 348   2            if( ( Get_PCx_input(7) )  == 0)
 349   2            {
 350   3                  PD1 = 0; // LED_G1 On
 351   3                  PD2 = 0 ; //LED_R On
C51 COMPILER V9.52.0.0   MAIN                                                              09/28/2022 19:51:28 PAGE 7   

 352   3                  PD3= 0;  // LED_G2  On
 353   3              
 354   3            }
 355   2          else
 356   2          {
 357   3                  PD1 = 1; // LED_G1 Off
 358   3                  PD2 = 1 ; //LED_R Off
 359   3                  PD3=  1;  // LED_G2  Off
 360   3          }
 361   2            
 362   2        }
 363   1      }
 364          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1025    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     42    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
